﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aribaccio;
using Aribaccio.Datum;
using BDPAutodetect.Models;
using Microsoft.EntityFrameworkCore;

namespace BDPAutodetect.DataService
{
    /// <summary>
    /// 数据库连接操作上下文
    /// </summary>
    public partial class CoreDBContext : DbContext, ICoreDBContext
    {
        /// <summary>
        /// 数据库连接类型模式
        /// </summary>
        public DBConnectType SQLConnectionType { get; set; }
        /// <summary>
        /// 数据库连接字符串[实现]
        /// </summary>
        public string SQLConnectionString { get; set; }

        /// <summary>
        /// 带参构造函数[注入]
        /// </summary>
        /// <param name="options"></param>
        public CoreDBContext(DbContextOptions<CoreDBContext> options)
            : base(options)
        {
            this.SQLConnectionString = string.Empty;
        }

        /// <summary>
        /// 数据库连接绑定[重写]
        /// </summary>
        /// <param name="argOptionsBuilder">DbContext选项构造器对象</param>
        protected override void OnConfiguring(DbContextOptionsBuilder argOptionsBuilder)
        {
            if (argOptionsBuilder.IsConfigured) return;
            if (string.IsNullOrEmpty(this.SQLConnectionString)) return;
            switch (this.SQLConnectionType)
            {
                case DBConnectType.SQLServer:
                    argOptionsBuilder.UseSqlServer(this.SQLConnectionString);
                    break;
                case DBConnectType.PostgreSQL:
                    argOptionsBuilder.UseNpgsql(this.SQLConnectionString);
                    break;
            }
        }

        /// <summary>
        /// 模型创建[重写]
        /// </summary>
        /// <param name="argModelBuilder">模型构造器对象</param>
        protected override void OnModelCreating(ModelBuilder argModelBuilder)
        {
            //对象关系映射配置[ORM]
            argModelBuilder.ApplyConfiguration(new TBasicSystemsConfiguration(this.SQLConnectionType));
            argModelBuilder.ApplyConfiguration(new TBasicUserConfiguration(this.SQLConnectionType));
            argModelBuilder.ApplyConfiguration(new TBasicRoleConfiguration(this.SQLConnectionType));
            argModelBuilder.ApplyConfiguration(new TBasicPowerConfiguration(this.SQLConnectionType));
            argModelBuilder.ApplyConfiguration(new TBasicRPRelationConfiguration(this.SQLConnectionType));
            argModelBuilder.ApplyConfiguration(new TBasicStationConfiguration(this.SQLConnectionType));
            argModelBuilder.ApplyConfiguration(new TBasicTargetConfiguration(this.SQLConnectionType));
            argModelBuilder.ApplyConfiguration(new TBasicSlaveConfiguration(this.SQLConnectionType));
            argModelBuilder.ApplyConfiguration(new TTargetSlaveConfiguration(this.SQLConnectionType));
            argModelBuilder.ApplyConfiguration(new TParameterConfiguration(this.SQLConnectionType));
            argModelBuilder.ApplyConfiguration(new TParameterLimitConfiguration(this.SQLConnectionType));
            argModelBuilder.ApplyConfiguration(new TDetectRecordDataConfiguration(this.SQLConnectionType));
            argModelBuilder.ApplyConfiguration(new TDetectRecordTotalConfiguration(this.SQLConnectionType));
            //对象关系映射扩展配置[ORM]
            OnModelCreatingPartial(argModelBuilder);
        }

        /// <summary>
        /// 模型创建[部分扩展]
        /// </summary>
        /// <param name="argModelBuilder">模型构造器对象</param>
        partial void OnModelCreatingPartial(ModelBuilder argModelBuilder);
    }
}
